package com.unicom.priceboard.data.hastc;

import java.io.*;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.unicom.priceboard.data.BaseXMLParser;
import com.unicom.priceboard.thread.HaStockThread;
import com.unicom.priceboard.util.MarketStateUtil;
import com.unicom.priceboard.util.NumberUtil;
import com.unicom.priceboard.data.vo.HaIndex;

public class HaIndexParser extends BaseXMLParser{
	
	
	private StringBuffer tempVal;
	
	//to maintain context
	private HaIndex tempInfo;
	
	
	public void parseDocument(String xmlFile) {
		super.parseDocument(xmlFile);
	}
	
	public void parseDocument(InputStream is) throws IOException{
		super.parseDocument(is);
	}


	//Event Handlers
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		//reset
		tempVal = new StringBuffer("");
		if(qName.equalsIgnoreCase("STS_MARKET_INFO")) {
			tempInfo = new HaIndex();
		}
	}
	

	public void characters(char[] ch, int start, int length) throws SAXException {
		tempVal.append(ch,start,length);
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if(qName.equalsIgnoreCase("STS_MARKET_INFO")) {
			//add it to the list
			if (HaIndex.HANOI_CENTER == tempInfo.getCenterId()) {
				//set index=0 new a day is not trading
				long traded=Long.valueOf(tempInfo.getTotalTrade().replaceAll(",",""));
				if(traded<=0){
					tempInfo.setIndex(0.0);
				}
				
				//Set marketState
				tempInfo.setMarketState(MarketStateUtil.getHASTC_state());
				
				HaIndex objInList = HaStockThread.mapMarketIndex.get(tempInfo.getIndexDate());
				if(objInList==null)
					HaStockThread.mapMarketIndex.put(tempInfo.getIndexDate(), tempInfo);
				else if(!objInList.equals(tempInfo)) {
					HaStockThread.mapMarketIndex.put(tempInfo.getIndexDate(), tempInfo);
				}
			}
			
		} else if (qName.equalsIgnoreCase("DATE_NO")) {
			HaStockThread.session=tempVal.toString();
			tempInfo.setSession(tempVal.toString());
		} else if (qName.equalsIgnoreCase("TRADING_DATE")) {
			if (tempVal.length() >= 10) {
				String []tradingDay = tempVal.substring(0, 10).split("-");
				tempInfo.setIndexDate(tradingDay[2]+"/"+tradingDay[1]+"/"+tradingDay[0]);
			}
		} else if (qName.equalsIgnoreCase("TOTAL_TRADE")) {
			tempInfo.setTotalTrade(tempVal.toString());
		} else if (qName.equalsIgnoreCase("MARKET_INDEX")) {
			tempInfo.setIndex(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("CHG_INDEX")) {
			tempInfo.setChangeIndex(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("PCT_INDEX")) {
			tempInfo.setPctIndex(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("FLOOR_CODE")) {
			tempInfo.setCenterId(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("TOTAL_QTTY")) {
			//tempInfo.setTotalVol(Long.parseLong(tempVal.toString()));
			tempInfo.setTotalVol(NumberUtil.formatVolume(Long.parseLong(tempVal.toString())));
		} else if (qName.equalsIgnoreCase("TOTAL_VALUE")) {
			tempInfo.setTotalVal(Long.parseLong(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("PT_TOTAL_QTTY")) {
			tempInfo.setPtTotalVol(Long.parseLong(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("PT_TOTAL_VALUE")) {
			tempInfo.setPtTotalVal(Long.parseLong(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("ADVANCES")) {
			tempInfo.setAdvances(Long.parseLong(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("DECLINES")) {
			tempInfo.setDeclines(Long.parseLong(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("NOCHANGE")) {
			tempInfo.setNoChange(Long.parseLong(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("TIME")) {
			tempInfo.setTime(tempVal.toString());
		} else if (qName.equalsIgnoreCase("CURRENT_STATUS")) {
			tempInfo.setMarketStatus(Integer.valueOf(tempVal.toString()));
		}
		
	}
}